﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using YellowMap.View.Control;
using System.Windows.Media;
using YellowMap.View.Page;

namespace YellowMap.View.Control
{
    public partial class FavoriteListItem : UserControl
    {
        public static FavoriteListItem Intance { get; set; }
        public static FavoriteListItem PressedItem { get; set; }
        double menuHeight = 237;
        public FavoriteListItem()
        {
            InitializeComponent();
            Intance = this;
            //Loaded += FavoriteListItem_Loaded;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                txtName.Foreground = new SolidColorBrush(Colors.Black);
                txtAddress.Foreground = new SolidColorBrush(Colors.Black);
            }
            else
            {
                txtName.Foreground = new SolidColorBrush(Colors.White);
                txtAddress.Foreground = new SolidColorBrush(Colors.White);
            }
            //txtAddress.Foreground = new SolidColorBrush((App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color);
            this.MouseEnter += FavoriteListItem_MouseEnter;
            this.MouseLeftButtonDown += FavoriteListItem_MouseLeftButtonDown;
            menu.Opened += menu_Opened;

        }

      

        

        double mouseY;
        void FavoriteListItem_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            mouseY = e.GetPosition(this).Y;
           // throw new NotImplementedException();
        }

        void menu_Opened(object sender, RoutedEventArgs e)
        {
            var listTop = VisualTreeHelper.FindElementsInHostCoordinates(new Rect(0, 0, 400, 400), FavoritesPage.Intance);
            if (listTop.Contains(this))
            {
                menu.VerticalOffset = ActualHeight - mouseY;
            }
            else
            {
                menu.VerticalOffset = -menuHeight - mouseY ;
            }
            //throw new NotImplementedException();
        }

        private void FavoriteListItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            PressedItem = this;
        }


        //void FavoriteListItem_Loaded(object sender, RoutedEventArgs e)
        //{
        //    UpdateTitleBG();
        //}

        //private void UpdateTitleBG()
        //{
        //    Favorite HistoryItem = DataContext as Favorite;
        //    string value = HistoryItem.SubCate;
        //    string[] cate = Regex.Split(value, "_");
        //    switch (cate[0])
        //    {
        //        case "foodDrink":
        //            titleBG.Fill = new SolidColorBrush(ConvertColorFromHexString(AppViewModel.Instance.Categories[0].Color));
        //            break;
        //        case "travel":
        //            titleBG.Fill = new SolidColorBrush(ConvertColorFromHexString(AppViewModel.Instance.Categories[1].Color));
        //            break;
        //        case "entertainment":
        //            titleBG.Fill = new SolidColorBrush(ConvertColorFromHexString(AppViewModel.Instance.Categories[2].Color));
        //            break;
        //        case "education":
        //            titleBG.Fill = new SolidColorBrush(ConvertColorFromHexString(AppViewModel.Instance.Categories[3].Color));
        //            break;
        //        case "shopping":
        //            titleBG.Fill = new SolidColorBrush(ConvertColorFromHexString(AppViewModel.Instance.Categories[4].Color));
        //            break;
        //        case "service":
        //            titleBG.Fill = new SolidColorBrush(ConvertColorFromHexString(AppViewModel.Instance.Categories[5].Color));
        //            break;
        //    }
        //}
        public static Color ConvertColorFromHexString(string hexaColor)
        {
            hexaColor = hexaColor.Trim(new char[] { '#' });
            if (hexaColor.Length == 6)
            {
                hexaColor += "ff";
            }

            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(6, 2), 16),
                    Convert.ToByte(hexaColor.Substring(0, 2), 16),
                    Convert.ToByte(hexaColor.Substring(2, 2), 16),
                    Convert.ToByte(hexaColor.Substring(4, 2), 16));
        }
     
        //private void UserControl_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        //{
        //    IsSelected = true;
        //}
        public static FavoriteListItem SelectedFavoriteListItem { get; set; }


        private bool _IsSelected;
        public bool IsSelected {
            get {
                return _IsSelected;

            }

            set {
                _IsSelected = value;
                if (value)
                {
                    if (SelectedFavoriteListItem != null)
                    {
                        SelectedFavoriteListItem.IsSelected = false;
                    }
                    if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                    {
                        VisualStateManager.GoToState(this, "selectDark", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(this, "selectLight", true);
                    }
                    SelectedFavoriteListItem = this;
                }
                else
                {
                    if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                    {
                        VisualStateManager.GoToState(this, "normalDark", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(this, "normalLight", true);
                    }
                }
            }
        }

        private void btnDelete_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            FavoritesPage.DetetedFavorite = DataContext as Favorite;
            FavoritesPage.Intance.DeleteItem();
        }

        //private void btnDelete_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        //{
        //    ImageRectangle.Opacity = 0.5;
        //}

        //private void btnDelete_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        //{
        //    ImageRectangle.Opacity = 0;
        //}

        //private void btnDelete_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        //{
        //    ImageRectangle.Opacity = 0.5;
        //}


        //Handle load pivot index 
        //private void pivotFavorites_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    Init(pivotFavorites.SelectedIndex);
        //}

       
        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            //switch (pivotFavorites.SelectedIndex)
            //{
            //    case 0:
            //        MainPage.SelectedFavorite = lbfoodDrink.SelectedItem as Favorite;
            //        break;
            //    case 1:
            //        MainPage.SelectedFavorite = lbtravel.SelectedItem as Favorite;
            //        break;
            //    case 2:
            //        MainPage.SelectedFavorite = lbentertainment.SelectedItem as Favorite;
            //        break;
            //    case 3:
            //        MainPage.SelectedFavorite = lbeducation.SelectedItem as Favorite;
            //        break;
            //    case 4:
            //        MainPage.SelectedFavorite = lbshopping.SelectedItem as Favorite;
            //        break;
            //    case 5:
            //        MainPage.SelectedFavorite = lbservice.SelectedItem as Favorite;
            //        break;

            //}
            AppViewModel.Instance.SettingVM.SelectedFavorite = FavoriteListItem.PressedItem.DataContext as Favorite;
            //MainPage.SelectedFavorite = lbfavorites.SelectedItem as Favorite;
            MainPage.COMMAND = MainPage_Command.SHOW_FAVORITE;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            FavoritesPage.Intance.NavigationService.GoBack();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            //switch (pivotFavorites.SelectedIndex)
            //{
            //    case 0:
            //        MainPage.SelectedFavorite = lbfoodDrink.SelectedItem as Favorite;
            //        break;
            //    case 1:
            //        MainPage.SelectedFavorite = lbtravel.SelectedItem as Favorite;
            //        break;
            //    case 2:
            //        MainPage.SelectedFavorite = lbentertainment.SelectedItem as Favorite;
            //        break;
            //    case 3:
            //        MainPage.SelectedFavorite = lbeducation.SelectedItem as Favorite;
            //        break;
            //    case 4:
            //        MainPage.SelectedFavorite = lbshopping.SelectedItem as Favorite;
            //        break;
            //    case 5:
            //        MainPage.SelectedFavorite = lbservice.SelectedItem as Favorite;
            //        break;

            //}

            //[pkhuy]
            if (AppViewModel.Instance.SettingVM.SearchFrom == Constant.SEARCHFROM_CHOOSEONMAP)
            {
                AppViewModel.Instance.SettingVM.MapLattitude = MainPage.Instance.CenterLocation.Latitude;
                AppViewModel.Instance.SettingVM.MapLongitude = MainPage.Instance.CenterLocation.Longitude;
            }

            AppViewModel.Instance.SettingVM.SelectedFavorite = FavoriteListItem.PressedItem.DataContext as Favorite;
            MainPage.COMMAND = MainPage_Command.SEARCH_FAVORITE;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            FavoritesPage.Intance.NavigationService.GoBack();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            FavoritesPage.DetetedFavorite = FavoriteListItem.PressedItem.DataContext as Favorite;
            FavoritesPage.Intance.DeleteItem();
        }

    }
}
